<cfcomponent extends="queries">

	<!---Need to call BASE functions--->
    <cfset BASE = CreateObject("component", "base") />
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcGetTermScheduleID" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>       
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            SELECT ID, TermReqID, SchedDate, Complete
            FROM TerminationSchedule
            WHERE 
                TermReqID = <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>
        <cfreturn qryGetScheduledTerms />            
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcInsertOUTermSchedule" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
        <cfargument default="-1" name="RosettaID" required="yes"/>
        <cfargument default="-1" name="ArchivedUserOU" required="yes"/>
        <cfargument default="-1" name="DeleteDate" required="yes"/>
        
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            Insert Into ArchivedUserOUTermSchedule (TermReqID, RosettaID, ArchivedUserOU, DeleteDate)
            Values (
                <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.RosettaID#" cfsqltype="cf_sql_integer" />,
                <cfqueryparam value="#ARGUMENTS.ArchivedUserOU#" cfsqltype="cf_sql_varchar" />,
                <cfqueryparam value="#ARGUMENTS.DeleteDate#" cfsqltype="cf_sql_date" />
            )
        </cfquery>
            
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcUpdateOUTermSchedule" returntype="any">
        <cfargument default="-1" name="ID" required="yes"/>
        <cfargument default="-1" name="Completed" required="yes"/>
        
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            Update ArchivedUserOUTermSchedule 
            SET
                Completed = <cfqueryparam value="#ARGUMENTS.Completed#" cfsqltype="cf_sql_varchar" />, 
            WHERE 
                ID = <cfqueryparam value="#ARGUMENTS.ID#" cfsqltype="cf_sql_integer" />
        </cfquery>
      
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcGetOUTermSchedule" returntype="any">
        <cfargument default="-1" name="ID" required="no"/>        
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            SELECT 
                TermReqID, RosettaID, ArchivedUserOU, DeleteDate, ID
            FROM 
                ArchivedUserOUTermSchedule
            <Cfif ARGUMENTS.ID NEQ -1>
	            WHERE 
	                ID = <cfqueryparam value="#ARGUMENTS.ID#" cfsqltype="cf_sql_integer" />
            </cfif>
        </cfquery>
        
        <cfreturn qryGetScheduledTerms />        
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcInsertArchiveFolderTermSchedule" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
        <cfargument default="-1" name="RosettaID" required="yes"/>
        <cfargument default="-1" name="ArchivedFolder" required="yes"/>
        <cfargument default="-1" name="DeleteDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            Insert Into ArchivedFolderTermSchedule (TermReqID, RosettaID, ArchivedFolder, DeleteDate)
            Values (
                <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.RosettaID#" cfsqltype="cf_sql_integer" />,
                <cfqueryparam value="#ARGUMENTS.ArchivedFolder#" cfsqltype="cf_sql_varchar" />,
                <cfqueryparam value="#ARGUMENTS.DeleteDate#" cfsqltype="cf_sql_date" />
            )
        </cfquery>
              
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcUpdateArchiveFolderTermSchedule" returntype="any">
        <cfargument default="-1" name="ID" required="yes"/>
        <cfargument default="-1" name="Completed" required="yes"/>
        
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            Update ArchivedFolderTermSchedule 
            SET
                Completed = <cfqueryparam value="#ARGUMENTS.Completed#" cfsqltype="cf_sql_varchar" />, 
            WHERE 
                ID = <cfqueryparam value="#ARGUMENTS.ID#" cfsqltype="cf_sql_integer" />
        </cfquery>
                
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    <!--- Get OUArchiveFolderTermSchedule --->
    <cffunction name="funcGetOUArchiveFolderTermSchedule" returntype="any">
        <cfargument default="-1" name="ID" required="no"/>        
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            SELECT 
                TermReqID, RosettaID, ArchivedFolder, DeleteDate, ID
            FROM 
                ArchivedFolderTermSchedule
            <Cfif ARGUMENTS.ID NEQ -1>
                WHERE 
                    ID = <cfqueryparam value="#ARGUMENTS.ID#" cfsqltype="cf_sql_integer" />
            </cfif>
        </cfquery>
        
        <cfreturn qryGetScheduledTerms />        
    </cffunction>
    <!--- Get OUArchiveFolderTermSchedule --->
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcScheduledTerms" returntype="any">
        <cfargument default="-1" name="TermDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryScheduledTerms">
            SELECT ID, TermReqID, SchedDate, SysTermDate, Complete
            FROM TerminationSchedule
            WHERE SchedDate <= '#DateFormat(ARGUMENTS.TermDate, "MM/DD/YYYY")#'  AND
                Complete = 0
        </cfquery>
        
        <cfreturn qryScheduledTerms />        
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcSysScheduledTerms" returntype="any">
        <cfargument default="-1" name="TermDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryScheduledTerms">
            SELECT ID, TermReqID, SchedDate, SysTermDate, Complete
            FROM TerminationSchedule
            WHERE SysTermDate <= '#DateFormat(ARGUMENTS.TermDate, "MM/DD/YYYY")#'  AND
                Complete = 0
        </cfquery>
        
        <cfreturn qryScheduledTerms />        
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    <!--- Get Check scheduled terms --->
    <cffunction name="funcGetScheduledTerms" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
        <cfargument default="-1" name="TermDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            SELECT ID, TermReqID, SchedDate, Complete
            FROM TerminationSchedule
            WHERE SchedDate = '#DateFormat(ARGUMENTS.TermDate, "M/D/YYYY")#' AND
                TermReqID = <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>
        
        <cfreturn qryGetScheduledTerms />        
    </cffunction>
    <!--- Get Check scheduled terms --->
    
    <!--- Get Check scheduled terms --->
    <cffunction name="funcGetSysScheduledTerms" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
        <cfargument default="-1" name="TermDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            SELECT ID, TermReqID, SysTermDate, Complete
            FROM TerminationSchedule
            WHERE SysTermDate = '#DateFormat(ARGUMENTS.TermDate, "M/D/YYYY")#' AND
                TermReqID = <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>
        
        <cfreturn qryGetScheduledTerms />        
    </cffunction>
    <!--- Get Check scheduled terms --->
    
    
    
    <!--- Get scheduled terms --->
    <cffunction name="funcCheckScheduledTerms" returntype="any">
        <cfargument default="-1" name="TermDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            SELECT ID, TermReqID, SchedDate, SysTermDate, Complete
            FROM TerminationSchedule
            WHERE SchedDate <= '#DateFormat(ARGUMENTS.TermDate, "MM/DD/YYYY")#' OR
                SysTermDate <= '#DateFormat(ARGUMENTS.TermDate, "MM/DD/YYYY")#'  AND
                Complete = 0
        </cfquery>
        
        <cfreturn qryGetScheduledTerms />        
    </cffunction>
    <!--- Get scheduled terms --->
    
    
    
    <!--- Update scheduled terms --->
    <cffunction name="funcUpdateScheduledTerms" returntype="any">
        <cfargument default="-1" name="TermScheduleID" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            UPDATE TerminationSchedule
            SET
                Complete = 1
            WHERE ID = <cfqueryparam value="#ARGUMENTS.TermScheduleID#" cfsqltype="cf_sql_integer" />
        </cfquery>
              
    </cffunction>
    <!--- Update scheduled terms --->
	
	
	
	<!--- Update CostCode terms --->
    <cffunction name="funcUpdateCostCode" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
		<cfargument default="-1" name="CostCode" required="yes"/>      
        <cfquery datasource="#Session.DataSource#" name="qryGetScheduledTerms">
            UPDATE TerminationRequests
            SET
                CostCode = <cfqueryparam value="#ARGUMENTS.CostCode#" cfsqltype="cf_sql_varchar" />
            WHERE ID = <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>
              
    </cffunction>
    <!--- Update CostCode terms --->
    
    
    
    <!--- Employee Step Detail Termination--->
    <cffunction name="funcInsertTermSchedule" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
        <cfargument default="-1" name="TermDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryInsertTermSchedule">
            Insert Into TerminationSchedule (TermReqID, SchedDate)
            Values (
                <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.TermDate#" cfsqltype="cf_sql_date" />
            )
            SELECT @@Identity as ID
        </cfquery>
        <cfreturn qryInsertTermSchedule />
    </cffunction>
    <!--- Employee Step Detail Termination--->

    <!--- Insert SysTerm schedule--->
    <cffunction name="funcInsertSysTermSchedule" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
        <cfargument default="-1" name="SysTermDate" required="yes"/>
        
        <cfquery datasource="#Session.DataSource#" name="qryInsertTermSchedule">
            Insert Into TerminationSchedule (TermReqID, SysTermDate)
            Values (
                <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />,
                <cfqueryparam value="#ARGUMENTS.SysTermDate#" cfsqltype="cf_sql_date" />
            )
            SELECT @@Identity as ID
        </cfquery>
        <cfreturn qryInsertTermSchedule />
    </cffunction>
    <!--- Employee Step Detail Termination--->
    
    
    <!--- INSERTS termination request record into EPAR DB --->
    <cffunction name="funcInsertTermanationRequestAction" returntype="any">
    	<cfargument default="-1" name="FORM" required="yes"/>
        
        <cfquery name="qryInsertTermanationRequestAction" datasource="#Session.DataSource#">
			<!---scf_InsertTermReq #intRosettaID#, #intEmpStatusID#, #ORG_SCF#, #intRehireElig#, #intTermTypeID#, #intESReasonID#, '#dteTerm#', '#dteCOBRA#'--->
            Insert Into TerminationRequests (RosettaID, EmpStatusIDNo, OrgCodeIDNo, RehireElig, TermTypeID, EmpStatusReasonCode, TermDate, SysTermDate, COBRAEndOfMonth, CostCode)
			Values (
            	<cfqueryparam value="#ARGUMENTS.FORM.RosettaID#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.FORM.EmpStatusIDNo#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="117" cfsqltype="cf_sql_integer" />,<!---WHat is this from scott's code.--->
                <cfif isDefined("ARGUMENTS.FORM.RehireElig")> 
                	<cfqueryparam value="1" cfsqltype="cf_sql_bit" />,
                <cfelse>
                	<cfqueryparam value="0" cfsqltype="cf_sql_bit" />,
                </cfif>
                <cfqueryparam value="#ARGUMENTS.FORM.TermTypeID#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.FORM.EmpStatusReasonCode#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.FORM.Term#" cfsqltype="cf_sql_date" />, 
                NULL, 
                <cfqueryparam value="#dateformat(dateadd('d',-1, dateadd('m',1,(Month(ARGUMENTS.FORM.Term) & '/1/' & Year(ARGUMENTS.FORM.Term)))),'mm/dd/yyyy')#" 
                    cfsqltype="cf_sql_date" />,
                NULL
            )			
            SELECT @@Identity AS TermID
        </cfquery>

        <cfreturn qryInsertTermanationRequestAction.TermID />        
    </cffunction>
    <!--- INSERTS termination request record into EPAR DB --->
        
    
    
    <!--- INSERTS termination files into system --->
    <cffunction name="funcInsertFile" returntype="any">
        <cfargument default="-1" name="EmpID" />
        <cfargument default="-1" name="TermReqID" />
        <cfargument default="" name="File" />
        <cfargument default="-1" name="TypeID" />
        <cfargument default="-1" name="SubmittedBy" />        
        
        <cfquery name="qryInsertFile" datasource="#Session.DataSource#">
            <!---scf_InsertFile NULL, #intTermReqID#, '#cffile.ServerFile#', #Form.FileType#, #Session.intUserRosettaID#--->
            Insert Into FileLibrary (EmployeeID, TermReqID, FileName, TypeID, LoadedBy, DateLoaded)
            Values(
                <cfif ARGUMENTS.EmpID NEQ -1>
                    <cfqueryparam value="#ARGUMENTS.EmpID#" cfsqltype="cf_sql_integer" />,
                <cfelse>
                    NULL,
                </cfif>
                <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.File#" cfsqltype="cf_sql_varchar" />, 
                <cfqueryparam value="#ARGUMENTS.TypeID#" cfsqltype="cf_sql_integer" />, 
                <cfqueryparam value="#ARGUMENTS.SubmittedBy#" cfsqltype="cf_sql_integer" />, 
                getDate()
            )
        </cfquery>
   
    </cffunction>
    <!--- INSERTS termination files into system --->
        
    
    
    <!--- UPDATES termination request record into EPAR DB --->
    <cffunction name="funcUpdateTermanationRequestAction" returntype="any">
        <cfargument default="-1" name="FORM" required="yes"/>
        
        <cfquery name="qryUpdateTermanationRequestAction" datasource="#Session.DataSource#">
            <!--- scf_UpdateTermReq #intTermReqID#, #intEmpStatusID#, #ORG_SCF#, #intRehireElig#, #intTermTypeID#, #intESReasonID#, '#dteTerm#', '#dteCOBRA#' - Scotts old code --->
			Update 
                TerminationRequests 
			Set 
                EmpStatusIDNo = <cfqueryparam value="#ARGUMENTS.FORM.EmpStatusIDNo#" cfsqltype="cf_sql_integer" />, 
			    OrgCodeIDNo = <cfqueryparam value="117" cfsqltype="cf_sql_integer" />,
                <Cfif isDefined("ARGUMENTS.FORM.RehireElig")>
                    RehireElig = <cfqueryparam value="1" cfsqltype="cf_sql_integer" />,
                <cfelse>
                    RehireElig = <cfqueryparam value="0" cfsqltype="cf_sql_integer" />,
                </cfif>
			    TermTypeID = <cfqueryparam value="#ARGUMENTS.FORM.TermTypeID#" cfsqltype="cf_sql_integer" />, 
			    EmpStatusReasonCode = <cfqueryparam value="#ARGUMENTS.FORM.EmpStatusReasonCode#" cfsqltype="cf_sql_integer" />, 
			    TermDate = <cfqueryparam value="#ARGUMENTS.FORM.TermDate#" cfsqltype="cf_sql_date" />, 
                SysTermDate = <cfqueryparam value="#ARGUMENTS.FORM.SysTermDate#" cfsqltype="cf_sql_date" />, 
			    COBRAEndOfMonth = <cfqueryparam value="#dateformat(dateadd('d',-1, dateadd('m',1,(Month(ARGUMENTS.FORM.TermDate) & '/1/' & Year(ARGUMENTS.FORM.TermDate)))),'mm/dd/yyyy')#" cfsqltype="cf_sql_date" />
			Where 
                ID = <cfqueryparam value="#ARGUMENTS.FORM.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>     
    </cffunction>
    <!--- UPDATES termination request record into EPAR DB --->
    
    
    
    <!--- UPDATES Termanation Dates EPAR DB --->
    <cffunction name="funcUpdateTermanationDates" returntype="any">
        <cfargument default="-1" name="FORM" required="yes"/>
        
        <cfquery name="getTermReq" datasource="#Session.DataSource#">
            Select * FROM TerminationRequests WHERE ID = <cfqueryparam value="#ARGUMENTS.FORM.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>
        
        <cfif Not IsDate(ARGUMENTS.FORM.TermDate)>
            <cfset ARGUMENTS.FORM.TermDate = getTermReq.TermDate />        
        </cfif>
        
        <cfif Not IsDate(ARGUMENTS.FORM.SysTermDate)>
            <cfset ARGUMENTS.FORM.SysTermDate = getTermReq.SysTermDate />        
        </cfif>
        
        <cfquery name="qryUpdateTermanationRequestAction" datasource="#Session.DataSource#">
            Update 
                TerminationRequests 
            Set 
                TermDate = <cfqueryparam value="#ARGUMENTS.FORM.TermDate#" cfsqltype="cf_sql_date" />, 
                SysTermDate = <cfqueryparam value="#ARGUMENTS.FORM.SysTermDate#" cfsqltype="cf_sql_date" />
            Where 
                ID = <cfqueryparam value="#ARGUMENTS.FORM.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>
        
        <Cfset ScheduledTerm = funcGetScheduledTerms( ARGUMENTS.FORM.TermReqID, getTermReq.TermDate ) />
        <Cfset SysScheduledTerm = funcGetSysScheduledTerms( ARGUMENTS.FORM.TermReqID, getTermReq.SysTermDate ) />
        
        <cfloop query="SysScheduledTerm">
            <cfquery name="qryUpdateTermanationSchedule" datasource="#Session.DataSource#">
                Update 
                    TerminationSchedule
                Set 
                    SysTermDate = <cfqueryparam value="#ARGUMENTS.FORM.SysTermDate#" cfsqltype="cf_sql_date" />
                Where 
                    ID = <cfqueryparam value="#SysScheduledTerm.ID#" cfsqltype="cf_sql_integer" />
            </cfquery>
        </cfloop>
        
        <cfloop query="ScheduledTerm">
            <cfquery name="qryUpdateTermanationSchedule" datasource="#Session.DataSource#">
                Update 
                    TerminationSchedule 
                Set 
                    SchedDate = <cfqueryparam value="#ARGUMENTS.FORM.TermDate#" cfsqltype="cf_sql_date" />
                Where 
                    ID = <cfqueryparam value="#ScheduledTerm.ID#" cfsqltype="cf_sql_integer" />
            </cfquery>
        </cfloop>
    </cffunction>
    <!--- UPDATES Termanation Dates EPAR DB --->
    
    
    
    <!--- DELETES termination request record from EPAR DB --->
    <cffunction name="funcDeleteTermanationStepAction" returntype="any">
        <cfargument default="-1" name="TermReqID" required="yes"/>
        
        <cfquery name="qryDeleteTermanationStepAction" datasource="#Session.DataSource#">
            DELETE
            FROM 
                StepActions
            WHERE 
                TermReqID = <cfqueryparam value="#ARGUMENTS.TermReqID#" cfsqltype="cf_sql_integer" />
        </cfquery>     
    </cffunction>
    <!--- DELETES termination request record from EPAR DB --->
    

</cfcomponent>